package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.internetofthings.chargingStation;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: YangYu
 * @ClassName: ChargingStationService
 * @Date: Created in 13:11 2023/12/21
 * @Description: 充电桩
 */
@Service
@Slf4j
public class ChargingStationService extends BaseService {

    /**
     * 首页数据
     * @param
     * @return
     */
    public RespEntity getHomePageData(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //汇总数据
        Map<String,Object> allMap = new HashMap<>();

        //今日报警
        jsonParam.put("deviceClassify","22");
        com.alibaba.fastjson.JSONObject todayAlarmObject = makeData(jsonParam,"fireFightingiotJava","config","todayAlarm","fireFightingIOT");
        if(StrUtil.isNotBlank(todayAlarmObject.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(todayAlarmObject.getString("methodErrorFlag"))) return RespEntity.fail(todayAlarmObject.getString("msg"));
        Map<String,Object> todayAlarm = new HashMap<>();
        todayAlarm.put("todayVoltageCount",todayAlarmObject.get("todayVoltageCount"));
        todayAlarm.put("todayCurrentCount",todayAlarmObject.get("todayCurrentCount"));
        todayAlarm.put("todayTemperatureCount",todayAlarmObject.get("todayTemperatureCount"));
        todayAlarm.put("todayLeakageCurrentCount",todayAlarmObject.get("todayLeakageCurrentCount"));
        todayAlarm.put("allCount",todayAlarmObject.get("allCount"));
        allMap.put("todayAlarm",todayAlarm);
        //一周报警
        JSON weekAlarmObject = makeDataForJSON(jsonParam,"fireFightingiotJava","config","weekAlarm","fireFightingIOT");
        if(weekAlarmObject instanceof com.alibaba.fastjson.JSONObject){
            com.alibaba.fastjson.JSONObject weekAlarmObjectN = (com.alibaba.fastjson.JSONObject)weekAlarmObject;
            if(StrUtil.isNotBlank(weekAlarmObjectN.getString("methodCodeFlag"))){
                return RespEntity.expire();
            }
            return RespEntity.fail(((com.alibaba.fastjson.JSONObject) weekAlarmObject).getString("msg"));
        }else{
            allMap.put("weekAlarmList",weekAlarmObject);
        }
        //设备统计
        jsonParam.put("deviceClassify","22");
        com.alibaba.fastjson.JSONObject todayDeviceCountByStateObject = makeData(jsonParam,"fireFightingiotJava","config","todayDeviceCountByState","device");
        if(StrUtil.isNotBlank(todayDeviceCountByStateObject.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(todayDeviceCountByStateObject.getString("methodErrorFlag"))) return RespEntity.fail(todayDeviceCountByStateObject.getString("msg"));
        allMap.put("todayDeviceCountByState",todayDeviceCountByStateObject);
        if(jsonParam.getStr("userType").equals("1")){
            //报警单位分析
            JSON alarmBusinessAnalysisObject = makeDataForJSON(jsonParam,"fireFightingiotJava","config","alarmBusinessAnalysis","fireFightingIOT");
            if(weekAlarmObject instanceof com.alibaba.fastjson.JSONObject){
                com.alibaba.fastjson.JSONObject JSONObjectalarmBusinessAnalysisObjectNN = (com.alibaba.fastjson.JSONObject)alarmBusinessAnalysisObject;
                if(StrUtil.isNotBlank(JSONObjectalarmBusinessAnalysisObjectNN.getString("methodCodeFlag"))){
                    return RespEntity.expire();
                }
                return RespEntity.fail(((com.alibaba.fastjson.JSONObject) alarmBusinessAnalysisObject).getString("msg"));
            }else{
                allMap.put("alarmBusinessAnalysisList",alarmBusinessAnalysisObject);
            }
            //联网单位统计
            com.alibaba.fastjson.JSONObject businessCount = makeData(jsonParam,"businessJava","config","businessCount","business");
            if(StrUtil.isNotBlank(businessCount.getString("methodCodeFlag"))) return RespEntity.expire();
            if(StrUtil.isNotBlank(businessCount.getString("methodErrorFlag"))) return RespEntity.fail(businessCount.getString("msg"));
            allMap.put("businessCount",businessCount);
        }
        //报警类型占比
        Map<String,Object> percentage = new HashMap<>();
        percentage.put("voltageProportion",todayAlarmObject.get("voltageProportion"));
        percentage.put("currentProportion",todayAlarmObject.get("currentProportion"));
        percentage.put("temperatureProportion",todayAlarmObject.get("temperatureProportion"));
        percentage.put("leakageCurrentProportion",todayAlarmObject.get("leakageCurrentProportion"));
        allMap.put("percentage",percentage);

        return RespEntity.success(allMap);
    }

    /**
     * 实时报警列表
     * @param
     * @return
     */
    public RespEntity realTimeAlarm(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        jsonParam.put("deviceClassify","22");
        return fireFightingiotJava(jsonParam,"realTimeAlarm","fireFightingIOT");
    }

    /**
     * 一周报警
     * @param
     * @return
     */
    public RespEntity weekAlarm(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        jsonParam.put("deviceClassify","22");
        return RespEntity.success(makeDataForJSON(jsonParam,"fireFightingiotJava","config","weekAlarm","fireFightingIOT"));
    }
}
